package com.todo.strategy.online.application.mq.sender;

import com.alibaba.fastjson.JSONObject;
import com.todo.component.mq.conf.StrategyRabbitConfig;
import com.todo.strategy.online.application.mq.entity.UStrategyStatusChangeEntity;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.UUID;

/**
 * @author azhebuxing
 * @date 2024/12/11 02:36
 * @description
 */
@Component
public class UStrategyMqSender {

    @Resource
    private AmqpTemplate rabbitTemplate;

    public void sendUStrategyStatusChange(UStrategyStatusChangeEntity entity, Integer delayTime) {
        if (delayTime == null || delayTime == 0) {
            rabbitTemplate.convertAndSend(StrategyRabbitConfig.EXCHANGE_STRATEGY, StrategyRabbitConfig.QUEUE_U_STRATEGY_STATUS, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        } else {
            rabbitTemplate.convertAndSend(StrategyRabbitConfig.EXCHANGE_STRATEGY, StrategyRabbitConfig.QUEUE_U_STRATEGY_STATUS, JSONObject.toJSONString(entity), message -> {
                message.getMessageProperties().setDelay(delayTime);
                message.getMessageProperties().setMessageId(UUID.randomUUID().toString());
                message.getMessageProperties().setDeliveryMode(MessageDeliveryMode.PERSISTENT);
                return message;
            });
        }
    }


}
